package pixelradar.util;

import net.minecraftforge.fml.common.Loader;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.List;

public class VersionCheck implements Runnable {
    private static boolean isLatestVersion = true;
    private static boolean isPKVersion = false;
    private static String latestVersion = "5";
    private static String updateURL = "";

    public static boolean isLatestVersion() {
        return isLatestVersion;
    }

    public static String getLatestVersion() {
        return latestVersion;
    }

    public static String getUpdateURL() {
        return updateURL;
    }

    public void run() {
        InputStream in = null;

        try {
            URL url = new URL("https://goo.gl/T20VFb");
            URLConnection con = url.openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
            con.setUseCaches(false);
            in = con.getInputStream();
        } catch (IOException var7) {
        }

        try {
            List<String> list = IOUtils.readLines(in, Charset.defaultCharset());
            int index = -100;

            for (int i = 0; i < list.size(); ++i) {
                if (((String) list.get(i)).contains(Loader.instance().getMCVersionString()) && ((String) list.get(i + 2)).contains("Pixelmon 8.2.0")) {
                    index = i;
                    break;
                }
            }

            String version = (String) list.get(index + 1);
            version = version.replace("\"modVersion\": \"", "");
            version = version.replace("\",", "");
            version = version.replace(" ", "");
            latestVersion = version;
            String pkVersion = (String) list.get(index + 2);
            pkVersion = pkVersion.replace("\"pkVersion\": \"", "");
            pkVersion = pkVersion.replace("\",", "");
            pkVersion = pkVersion.replace(" ", "");
            pkVersion = pkVersion.replace("Pixelmon", "");
            String updateURL = (String) list.get(index + 4);
            updateURL = updateURL.replace("\"updateURL\": \"", "");
            updateURL = updateURL.replace("\",", "");
            updateURL = updateURL.replace(" ", "");
            isLatestVersion = "5.3".equals(version);
        } catch (Exception var8) {
        }

    }
}
